﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoSingleton<ObjectPool>
{    
    private List<GameObject> commonPlatformList = new List<GameObject>();
    private List<GameObject> obstaclePlatformList = new List<GameObject>();    
    private List<GameObject> SpikePlatformList = new List<GameObject>();
    private List<GameObject> playerDieEffectList = new List<GameObject>();
    private List<GameObject> diamondList = new List<GameObject>();
   

    private GameObject work;
    private GameObject idle;
   

    protected override void Awake()
    {
        base.Awake();
        work = GameObject.Find("Pool/work");
        idle = GameObject.Find("Pool/idle");       
    }
   

    /// <summary>
    /// 从对象池取出，激活至work
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public GameObject GetObj(string name)
    {
        GameObject get = null;
        GameObject prefab = ResManager.Instance.GetRes("Prefabs/" + name) as GameObject;     
        switch (prefab.tag)
        {
            case "CommonPlatform":
                if (commonPlatformList.Count > 0)
                {
                    get = commonPlatformList[0];
                    commonPlatformList.RemoveAt(0);
                    get.SetActive(true);
                }
                else
                {
                    get = Instantiate(prefab);                    
                }
                break;
            case "ObstaclePlatform":
                if (obstaclePlatformList.Count > 0)
                {
                    get = obstaclePlatformList[0];
                    obstaclePlatformList.RemoveAt(0);
                    get.SetActive(true);
                }
                else
                {
                    get = Instantiate(prefab);
                }
                break;         
            case "SpikePlatform":
                if (SpikePlatformList.Count > 0)
                {
                    get = SpikePlatformList[0];
                    SpikePlatformList.RemoveAt(0);
                    get.SetActive(true);
                }
                else
                {
                    get = Instantiate(prefab);
                }
                break;
            case "PlayerDieEffect":
                if (playerDieEffectList.Count > 0)
                {
                    get = playerDieEffectList[0];
                    playerDieEffectList.RemoveAt(0);
                    get.SetActive(true);
                }
                else
                {
                    get = Instantiate(prefab);
                }
                break;
            case "Diamond":
                if (diamondList.Count > 0)
                {
                    get = diamondList[0];
                    diamondList.RemoveAt(0);
                    get.SetActive(true);
                }
                else
                {
                    get = Instantiate(prefab);
                }
                break;       
            default:
                break;
        }
        get.transform.SetParent(work.transform);
        return get;
    }

    /// <summary>
    /// 放回对象池，隐藏至idle
    /// </summary>
    public void BackObj(GameObject obj)
    {
        obj.SetActive(false);
        obj.transform.SetParent(idle.transform);
        switch (obj.tag)
        {
            case "CommonPlatform":
                commonPlatformList.Add(obj);
                break;
            case "ObstaclePlatform":
                obstaclePlatformList.Add(obj);
                break;           
            case "SpikePlatform":
                SpikePlatformList.Add(obj);
                break;
            case "PlayerDieEffect":
                playerDieEffectList.Add(obj);
                break;
            case "Diamond":
                diamondList.Add(obj);
                break;           
            default:
                break;
        }        
    }
}